﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EcSalseManage.App_Code;

namespace EcSalseManage.EcSalse
{
    public partial class Report_ChangeOperater_Detail : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (!CommonFunction.isInPrev("Report_ChangeOperater_0001"))
                {
                    Response.Redirect("../Error/Perm_Error.aspx");
                }
                if (!CommonFunction.isInPrev("Report_ChangeOperater_0002"))
                {
                    this.btnSave.Enabled = false;
                }

                if (!CommonFunction.isInPrev("Report_ChangeOperater_0003"))
                {
                    this.btnDelete.Enabled = false;
                }

                if (Request.QueryString["id"] != null)
                {
                    ViewState["id"] = Request.QueryString["id"].ToString();
                    Update_Data_Bind(ViewState["id"].ToString());
                    btnReset.Visible = false;
                    btnDelete.Visible = true;
                    this.Report_Tab1.Visible = true;
                    this.Report_Tab1.MainID = ViewState["id"].ToString();
                }
                else if(Session[""]!=null)
                {
                    DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
                    this.lblProjectName.Text = Project_Inherit.getProjectName(Session["ProjectID"].ToString());
                    ViewState["id"] = null;
                    this.btnAudit.Visible = false;
                }
            }
        }

        private void Update_Data_Bind(string id)
        {
            DataAccess.Operate.DA_Report_ChangeOperater_Operate Report_ChangeOperater_Operate = new DataAccess.Operate.DA_Report_ChangeOperater_Operate();
            DataEntity.T_Report_ChangeOperater Report_ChangeOperater = new DataEntity.T_Report_ChangeOperater();
            Report_ChangeOperater = (DataEntity.T_Report_ChangeOperater)Report_ChangeOperater_Operate.SelectOne(id);
            this.txtReport_ChangeOperater_ExchangeDate.Text = Report_ChangeOperater.Report_ChangeOperater_ExchangeDate.ToString();
            this.txtReport_ChangeOperater_FormlyerOperater.Text = Report_ChangeOperater.Report_ChangeOperater_FormlyerOperater.ToString();
            this.txtReport_ChangeOperater_NowOperater.Text = Report_ChangeOperater.Report_ChangeOperater_NowOperater.ToString();
            this.txtReport_ChangeOperater_Price.Text = Report_ChangeOperater.Report_ChangeOperater_Price.ToString();
            this.txtReport_ChangeOperater_Reason.Text = Report_ChangeOperater.Report_ChangeOperater_Reason.ToString();
            this.txtReport_ChangeOperater_ReportNO.Text = Report_ChangeOperater.Report_ChangeOperater_ReportNO.ToString();
            this.txtReport_ChangeOperater_Unit.Text = Report_ChangeOperater.Report_ChangeOperater_Unit.ToString();

            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            this.lblProjectName.Text = Project_Inherit.getProjectName(Report_ChangeOperater.Report_ChangeOperater_ProjectID.ToString());
            ViewState["ProjectID"] = Report_ChangeOperater.Report_ChangeOperater_ProjectID.ToString();

            if (Report_ChangeOperater.Report_Plan_Audit > 38)
            {
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
            }
            if (Report_ChangeOperater.Report_Plan_Audit == 38)
                this.btnAudit.Text = "项经审核";
            if (Report_ChangeOperater.Report_Plan_Audit == 39)
                this.btnAudit.Text = "营销[副]总监审核";
            if (Report_ChangeOperater.Report_Plan_Audit == 40)
            {
                this.btnAudit.Text = "审核已完成";
                this.btnAudit.Enabled = false;
            }

            if (!CommonFunction.isInPrev("Report_ChangeOperater_0004") && this.btnAudit.Text == "项经审核")
            {
                this.btnAudit.Enabled = false;
            }
            if (!CommonFunction.isInPrev("Report_ChangeOperater_0005") && this.btnAudit.Text == "营销[副]总监审核")
            {
                this.btnAudit.Enabled = false;
            }
 

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_ChangeOperater_Operate Report_ChangeOperater_Operate = new DataAccess.Operate.DA_Report_ChangeOperater_Operate();
            DataEntity.T_Report_ChangeOperater Report_ChangeOperater = new DataEntity.T_Report_ChangeOperater();
            Report_ChangeOperater.Report_ChangeOperater_ExchangeDate = Convert.ToDateTime(this.txtReport_ChangeOperater_ExchangeDate.Text.Trim());
            Report_ChangeOperater.Report_ChangeOperater_ExchangeTypeID = 10;
            Report_ChangeOperater.Report_ChangeOperater_FormlyerOperater = Convert.ToDecimal(this.txtReport_ChangeOperater_FormlyerOperater.Text.Trim());
            Report_ChangeOperater.Report_ChangeOperater_NowOperater = Convert.ToDecimal(this.txtReport_ChangeOperater_NowOperater.Text.Trim());
            Report_ChangeOperater.Report_ChangeOperater_Price = Convert.ToDecimal(this.txtReport_ChangeOperater_Price.Text.Trim());
            Report_ChangeOperater.Report_ChangeOperater_Reason = this.txtReport_ChangeOperater_Reason.Text.Trim();
            Report_ChangeOperater.Report_ChangeOperater_ReportNO = this.txtReport_ChangeOperater_ReportNO.Text.Trim();
            Report_ChangeOperater.Report_ChangeOperater_Unit = this.txtReport_ChangeOperater_Unit.Text.Trim();
            Report_ChangeOperater.Report_Plan_Audit = 1;
            Guid gReport_ID = System.Guid.NewGuid();
            if (ViewState["id"]==null)
            {
                Report_ChangeOperater.Report_ChangeOperater_ID = gReport_ID;
                if (Report_ChangeOperater_Operate.Insert(Report_ChangeOperater))
                {
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, gReport_ID, 1);
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_INSERT_SUCCESS+"');</script>");
                    SendMessage("10", ViewState["ProjectID"].ToString());
                    ClearForm();
                }
                else
                {
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_INSERT_FAILS+"');</script>");
                }
            }
            else
            {
                Report_ChangeOperater.Report_ChangeOperater_ID = new Guid(ViewState["id"].ToString());
                if (Report_ChangeOperater_Operate.Update(Report_ChangeOperater))
                {
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_UPDATE_SUCCESS+"');</script>");
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, new Guid(ViewState["id"].ToString()), 2);

                }
                else
                {
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_UPDATE_FAILS+"');</script>");
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_ChangeOperater_Operate Report_ChangeOperater_Operate = new DataAccess.Operate.DA_Report_ChangeOperater_Operate();
            if (Report_ChangeOperater_Operate.Delete(ViewState["id"].ToString()))
            {
                Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_DELETE_SUCCESS+"');window.close();</script>");
                DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, new Guid(ViewState["id"].ToString()), 3);

            }
            else
            {
                Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_DELETE_FAILS+"');</script>");
            }
        }

        private void ClearForm()
        {
            Control frm = FindControl("Form1");
            foreach (Control ctrl in frm.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = " ";
                }
            }
        }

        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (this.btnAudit.Text == "项经审核")
            {
              
                DataAccess.Operate.DA_Report_ChangeOperater_Inherit Report_ChangeOperater_Inherit = new DataAccess.Operate.DA_Report_ChangeOperater_Inherit();
                if (Report_ChangeOperater_Inherit.AuditByID(ViewState["id"].ToString(), "39"))
                {
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 6, new Guid(ViewState["id"].ToString()), 10);
                    Response.Write("<script>alert('审核成功');window.close();</script>");
                    SendMessage("5", ViewState["ProjectID"].ToString());
                }
            }
            else if (this.btnAudit.Text == "营销[副]总监审核")
            {
                DataAccess.Operate.DA_Report_ChangeOperater_Inherit Report_ChangeOperater_Inherit = new DataAccess.Operate.DA_Report_ChangeOperater_Inherit();
                if (Report_ChangeOperater_Inherit.AuditByID(ViewState["id"].ToString(), "40"))
                {
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 6, new Guid(ViewState["id"].ToString()), 5);
                    Response.Write("<script>alert('审核成功');window.close();</script>");
                }
            }
        }

        public void SendMessage(string operate, string project)
        {
            string projectNo = "";
            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            string[] array = Project_Inherit.getCodeAndName(out projectNo, operate, project);
            string sTitle = "请注意，项目编号:" + projectNo + "中的变更销售人员经手人需要您的审批。";
            string sBody = "请注意，项目编号:" + projectNo + " 中的变更销售人员经手人需要您的审批。<a href='http://wap.gzcentaline.com.cn'>点击此处</a>";
            // array[1] 姓名 ，array[0] 工号
            EcSalseManage.App_Code.CommonFunction.SendMessage(array[1], array[0], "", "");

        }




    }
}
